package bb.lanxing.devicelist;

import static bb.lanxing.common.config.Constants.REQUEST_CODE;

import android.content.Intent;
import android.text.TextUtils;

import androidx.activity.ComponentActivity;
import androidx.lifecycle.ViewModelLazy;
import androidx.lifecycle.ViewModelProvider;
import androidx.lifecycle.ViewModelStore;
import androidx.lifecycle.viewmodel.CreationExtras;

import bb.lanxing.App;
import bb.lanxing.R;
import bb.lanxing.activity.login.PhoneBindActivity;
import bb.lanxing.databinding.ActivitySgotherDeviceBinding;
import bb.lanxing.lib.devices.base.DeviceContext;
import bb.lanxing.model.database.User;
import bb.lanxing.mvvm.base.BaseDBActivity;
import bb.lanxing.util.SensorEnableUtil;
import kotlin.Lazy;
import kotlin.jvm.functions.Function0;
import kotlin.jvm.internal.Reflection;


public class XZOtherDeviceActivity extends BaseDBActivity<ActivitySgotherDeviceBinding> {
    private final Lazy viewModel$delegate;

    @Override
    public int getLayoutId() {
        return R.layout.activity_sgother_device;
    }

    public XZOtherDeviceActivity() {
        XZOtherDeviceActivity xZOtherDeviceActivity = this;
        this.viewModel$delegate = new ViewModelLazy(Reflection.getOrCreateKotlinClass(XZDeviceListViewModel.class),
                new getStoreProducer(xZOtherDeviceActivity),
                new getFactoryProducer(xZOtherDeviceActivity),
                new getExtrasProducer(null, xZOtherDeviceActivity));
    }

    public XZDeviceListViewModel getViewModel() {
        return (XZDeviceListViewModel) this.viewModel$delegate.getValue();
    }

    @Override
    public void initView(ActivitySgotherDeviceBinding binding) {
        setupActionBar(true);
        setTitle(R.string.mine_title_device);
    }

    public void biciBike() {
        if (!App.getContext().isUserSignin()) {
            App.getContext().userSignin();
        } else if ((!SensorEnableUtil.isSdkVersionAboveS() && SensorEnableUtil.isSdkVersionAboveM() && !SensorEnableUtil.checkGPSPermission(this)) || !SensorEnableUtil.checkGpsEnabled(this)) {
        } else {
            if (DeviceContext.isBiciDebuggable() && !getViewModel().isConnected(1)) {
                //TODO:hu
//                startActivity(new Intent(this, DeviceListActivity.class).putExtra("type", 1));
            } else if (DeviceContext.getDeviceByType(1) == null) {
                User signinUser = App.getContext().getSigninUser();
                if (signinUser != null && TextUtils.isEmpty(signinUser.getPhone())) {
                    startActivityForResult(new Intent(this, PhoneBindActivity.class).putExtra("user_id", signinUser.getUid()), REQUEST_CODE);
                } else {
                    //TODO:hu
//                    startActivity(new Intent(this, DeviceListActivity.class).putExtra("type", 1));
                }
            } else {
                //TODO:hu
//                startActivity(new Intent(this, BiciStatusActivity.class));
            }
        }
    }

    public class getFactoryProducer implements Function0<ViewModelProvider.Factory> {
        ComponentActivity $this_viewModels;

        public getFactoryProducer(ComponentActivity componentActivity) {
            super();
            this.$this_viewModels = componentActivity;
        }

        @Override
        public ViewModelProvider.Factory invoke() {
            ViewModelProvider.Factory defaultViewModelProviderFactory = this.$this_viewModels.getDefaultViewModelProviderFactory();
//            Intrinsics.checkNotNullExpressionValue(defaultViewModelProviderFactory, "defaultViewModelProviderFactory");
            return defaultViewModelProviderFactory;
        }
    }

    public class getStoreProducer implements Function0<ViewModelStore> {
        ComponentActivity $this_viewModels;

        public getStoreProducer(ComponentActivity componentActivity) {
            super();
            this.$this_viewModels = componentActivity;
        }

        @Override
        public ViewModelStore invoke() {
            ViewModelStore viewModelStore = this.$this_viewModels.getViewModelStore();
//            Intrinsics.checkNotNullExpressionValue(viewModelStore, "viewModelStore");
            return viewModelStore;
        }
    }


    public class getExtrasProducer implements Function0<CreationExtras> {
        Function0 $extrasProducer;
        ComponentActivity $this_viewModels;

        public getExtrasProducer(Function0 function0, ComponentActivity componentActivity) {
            super();
            this.$extrasProducer = function0;
            this.$this_viewModels = componentActivity;
        }

        @Override
        public CreationExtras invoke() {
            CreationExtras creationExtras;
            Function0 function0 = this.$extrasProducer;
            if (function0 == null || (creationExtras = (CreationExtras) function0.invoke()) == null) {
                CreationExtras defaultViewModelCreationExtras = this.$this_viewModels.getDefaultViewModelCreationExtras();
                return defaultViewModelCreationExtras;
            }
            return creationExtras;
        }
    }
}
